package com.dev.module_zqc_sleep.fragment

import android.app.Activity
import android.content.Intent
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.dev.module_ui_composable.theme.JetComposableTheme
import com.dev.module_ui_composable.view.AbstractComposeFragment
import com.dev.module_ui_composable.view.ad.BannerAdView
import com.dev.module_zqc_sleep.R
import com.dev.module_zqc_sleep.theme.ColorFFF7F5FF


@Route(path = "/module_sleep/route/SecondFragment")
class SecondFragment : AbstractComposeFragment() {
    @Composable
    override fun ComposeContent() {
        JetComposableTheme(useSystemBarColor = false) {
            SecondScreen(
                onNavigate = { index ->

                },
                onNavigateA = { index ->

                },
                onNavigateB = { index ->

                }
            )
        }
    }

    private inline fun <reified T : Activity> navigateActivity() {
        val intent = Intent(requireContext(), T::class.java)
        startActivity(intent)
    }
}

@Composable
private fun SecondScreen(
    onNavigate: (Int) -> Unit,
    onNavigateA: (Int) -> Unit,
    onNavigateB: (Int) -> Unit
) {
    val icons1 = remember {
        mutableListOf(R.mipmap.a_sjjsq_sy_tub_6, R.mipmap.a_sjjsq_sy_tub_2, R.mipmap.a_xj_yyhh_3)
    }
    val icons2 = remember {
        mutableListOf(R.mipmap.a_sjjsq_sy_tub_4, R.mipmap.a_sjjsq_sy_tub_5)
    }

    Scaffold(
        modifier = Modifier.fillMaxSize(),
        containerColor = ColorFFF7F5FF,
        contentWindowInsets = WindowInsets(0, 0, 0, 0)
    ) { paddingValues ->
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(paddingValues)
                .verticalScroll(rememberScrollState())
        ) {
            Image(
                painter = painterResource(R.mipmap.a_banner_1),
                contentDescription = null,
                modifier = Modifier.fillMaxWidth(),
                contentScale = ContentScale.FillWidth
            )

            Surface(
                onClick = { onNavigate(0) },
                modifier = Modifier
                    .fillMaxWidth()
                    .height(80.dp)
                    .padding(horizontal = 15.dp),
                color = Color.White,
                shape = RoundedCornerShape(10.dp)
            ) {
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(horizontal = 25.dp, vertical = 10.dp),
                    horizontalArrangement = Arrangement.spacedBy(10.dp)
                ) {
                    Image(
                        painter = painterResource(R.mipmap.a_xj_pz_1),
                        contentDescription = null,
                        modifier = Modifier.wrapContentSize(),
                        contentScale = ContentScale.FillBounds
                    )

                    Column(verticalArrangement = Arrangement.spacedBy(10.dp, Alignment.CenterVertically)) {
                        Text(text = "沉浸时刻", fontSize = 18.sp)
                        Text(text = "没有噪音的舒适", fontSize = 12.sp)
                    }
                }
            }

            Spacer(modifier = Modifier.height(10.dp))
            
            Surface(
                onClick = { onNavigate(1) },
                modifier = Modifier
                    .fillMaxWidth()
                    .height(80.dp)
                    .padding(horizontal = 15.dp),
                color = Color.White,
                shape = RoundedCornerShape(10.dp)
            ) {
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(horizontal = 25.dp, vertical = 10.dp),
                    horizontalArrangement = Arrangement.spacedBy(10.dp)
                ) {
                    Image(
                        painter = painterResource(R.mipmap.a_ship_xj_2),
                        contentDescription = null,
                        modifier = Modifier.wrapContentSize(),
                        contentScale = ContentScale.FillBounds
                    )

                    Column(verticalArrangement = Arrangement.spacedBy(10.dp, Alignment.CenterVertically)) {
                        Text(text = "有氧呼吸", fontSize = 18.sp)
                        Text(text = "清晰的环境下接触大自然", fontSize = 12.sp)
                    }
                }
            }

            Spacer(modifier = Modifier.height(50.dp))

            Column(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(15.dp),
                verticalArrangement = Arrangement.spacedBy(12.dp)
            ) {
                Text(text = "小工具箱", fontSize = 18.sp, fontWeight = FontWeight.Bold)
                Spacer(modifier = Modifier.height(3.dp))

                Row(
                    modifier = Modifier.fillMaxWidth(),
                    horizontalArrangement = Arrangement.spacedBy(10.dp, Alignment.CenterHorizontally)
                ) {
                    icons1.forEachIndexed { index, resId -> 
                        Image(
                            painter = painterResource(resId),
                            contentDescription = null,
                            modifier = Modifier
                                .weight(1f)
                                .clickable { onNavigateA(index) },
                            contentScale = ContentScale.FillWidth
                        )
                    }
                }

                Row(
                    modifier = Modifier.fillMaxWidth(),
                    horizontalArrangement = Arrangement.spacedBy(10.dp, Alignment.CenterHorizontally)
                ) {
                    icons2.forEachIndexed { index, resId ->
                        Image(
                            painter = painterResource(resId),
                            contentDescription = null,
                            modifier = Modifier
                                .weight(1f)
                                .clickable { onNavigateB(index) },
                            contentScale = ContentScale.FillWidth
                        )
                    }
                }
            }

            BannerAdView()
        }
    }
}